package com.yltrcc.blog.controller.api;

import com.yltrcc.blog.model.model.JsonRootBean;
import com.yltrcc.blog.generate.domain.BlogArticleModel;
import com.yltrcc.blog.generate.domain.Question;
import com.yltrcc.blog.model.enums.ArticleStatus;
import com.yltrcc.blog.model.enums.PostType;
import com.yltrcc.blog.business.ArticleService;
import com.yltrcc.blog.business.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Random;

/**
 * Package: com.yltrcc.blog.api
 * Date：2022-01-17
 * Time：15:27
 * Description：TODO
 *
 * @author yltrcc
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/api/random")
public class RandomApiController {
    @Autowired
    private QuestionService questionService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 获取Markdown格式的文章，用于微信公众号
     *
     * @return
     */
    @GetMapping("/getArticle")
    public String getArticle() {

        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        //已发表文章
        int articleCount = articleService.countByStatus(ArticleStatus.PUBLISH.getStatus(), PostType.POST_TYPE_POST.getValue());
        BlogArticleModel blogArticleModel = null;

        while (null == blogArticleModel) {
            int articleId = random.nextInt(articleCount);
            articleId = articleId == 0 ? 1 : articleId;
            System.out.println("articleId = " + articleId);
            blogArticleModel = articleService.findByArticleId(articleId);
        }
        sb.append("# ");
        sb.append(blogArticleModel.getArticleTitle());
        sb.append("\n\n");
        //每日一句
        JsonRootBean user = restTemplate.getForObject("https://v1.hitokoto.cn/", JsonRootBean.class);
        sb.append("# 每日一句\n\n");
        sb.append(user.getHitokoto());
        sb.append("\n\n");
        sb.append(blogArticleModel.getArticleContentMd());
        //面试题
        int count = questionService.getCounts();

        if (count == 0) {
            return "Sorry, 没有数据，请添加数据";
        }

        int nextInt = random.nextInt(count);
        nextInt = nextInt == 0 ? 1 : nextInt;
        System.out.println("nextInt = " + nextInt);
        Question question = questionService.findByQuestionId(nextInt);
        sb.append("\n\n\n# 面试题\n");
        sb.append(question.getArticleContentMd());
        sb.append("\n\n");
        for (int i = 0; i < 2; i++) {
            //获取随机数
            int i2 = random.nextInt(count);
            i2 = i2 == 0 ? 1 : i2;
            System.out.println("i2 = " + i2);
            Question question1 = questionService.findByQuestionId(i2);
            sb.append(question1.getArticleContentMd());
            sb.append("\n\n");
        }
        //文末微信公众号推广连接
        sb.append("\n\n> 你好，我是yltrcc，日常分享技术点滴，欢迎关注我：[ylcoder](https://mp.weixin.qq.com/s/-GWOmQA5QFMF0OLcnGk7qQ)");
        return sb.toString();
    }
}
